package com.ruoyi.charge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.ruoyi.charge.bo.AgentRAddBo;
import com.ruoyi.charge.bo.AgentREditBo;
import com.ruoyi.charge.bo.AgentRQueryBo;
import com.ruoyi.charge.domain.Agent;
import com.ruoyi.charge.domain.AgentR;
import com.ruoyi.charge.mapper.AgentMapper;
import com.ruoyi.charge.mapper.AgentRMapper;
import com.ruoyi.charge.service.IAgentRService;
import com.ruoyi.charge.vo.AgentRVo;
import com.ruoyi.common.enums.DeletedStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代理关系Service业务层处理
 *
 * @author fanqie
 * @date 2021-10-25
 */
@Service
public class AgentRServiceImpl extends ServiceImpl<AgentRMapper, AgentR> implements IAgentRService {

    @Autowired
    private AgentMapper agentMapper;

    @Override
    public AgentRVo queryById(Long id) {
        AgentR db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, AgentRVo.class);
    }

    @Override
    public List<AgentRVo> queryList(AgentRQueryBo bo) {
        LambdaQueryWrapper<AgentR> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getAgentId() != null, AgentR::getAgentId, bo.getAgentId());
        lqw.eq(bo.getChildId() != null, AgentR::getChildId, bo.getChildId());
        return entity2Vo(this.list(lqw));
    }

    /**
     * 实体类转化成视图对象
     *
     * @param collection 实体类集合
     * @return
     */
    private List<AgentRVo> entity2Vo(Collection<AgentR> collection) {
        List<AgentRVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, AgentRVo.class))
                .collect(Collectors.toList());
        voList.forEach(e -> {
            Long childId = e.getChildId();
            Agent agent = agentMapper.selectOne(
                    new LambdaQueryWrapper<Agent>().eq(Agent::getAgentId, childId)
                    .eq(Agent::getDeleted, DeletedStatus.VALID.getCode())
                    .last("limit 1")
            );
            if(agent!=null){
                e.setChileNumber(agent.getAgentNumber());
                e.setChildName(agent.getAgentName());
            }
        });
        if (collection instanceof Page) {
            Page<AgentR> page = (Page<AgentR>) collection;
            Page<AgentRVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(AgentRAddBo bo) {
        AgentR add = BeanUtil.toBean(bo, AgentR.class);
        validEntityBeforeSave(add);
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(AgentREditBo bo) {
        AgentR update = BeanUtil.toBean(bo, AgentR.class);
        validEntityBeforeSave(update);
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(AgentR entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }
}
